const STATE = {
    CREATE : 1,
    LOADING : 2,
    SUCCESS : 3,
    FAIL : 4
}

class Res{
    constructor( params ){
    // {
    //     type : 'SpriteFrame' | 'SpriteAtlas' | 'Text' | 'PreFab',
    //     url : "" | {url :'' , plist : ''},
    //     path : ''
    // }
        this._loadedHandlers = [];
        this._data = params ? Object.assign({},params) : {};
        this.__state = STATE.CREATE;
        this._res = null;
        this.init();
    }


    get v(){
        return new Promise(( res , rej)=>{
            if( this.__state===STATE.SUCCESS && this._res ){
                res( this._res );
            }else if( this.__state===STATE.LOADING ){
                this._addHandler(()=>{
                    res( this._res );
                })
            }else{
                this._addHandler(()=>{
                    res( this._res );
                })
                this._load();
            }
        });
    }

    get res(){
        return this._res;
    }
    
    init(){
        const _d = this._data;
    }
    
    _load(){
        const _d = this._data;
        let loadFn =  _d['path'] ? cc.loader.loadRes : cc.loader.load;
        let route = _d['path'] ? _d['path'] : _d['url'];

        //未指定路径，则直接返回
        if(!route) return;

        if( _d['type'] == "SpriteAtlas" ){
            this.__state = STATE.LOADING;
            loadFn.call( cc.loader,  route, cc.SpriteAtlas , ( err , tex )=>{
                if(err){
                    console.log(err);
                    console.log(route);
                    this.__state = STATE.FAIL;
                }else{
                    //let atlas =new cc.SpriteAtlas(tex);
                    this._res = tex;
                    this.__state = STATE.SUCCESS;
                }

                this._doHandlers();
            } );
        }else if( _d['type'] == "SpriteFrame" ){
            this.__state = STATE.LOADING;
            loadFn.call( cc.loader,  route , ( err , tex )=>{
                if(err){
                    console.log(err);
                    console.log(route);
                    this.__state = STATE.FAIL;
                }else{
                    let atlas = new cc.SpriteFrame(tex);
                    this._res = atlas;
                    this.__state = STATE.SUCCESS;
                }

                this._doHandlers();
            });
        }else if(  _d['type'] == "AudioClip" ){
            this.__state = STATE.LOADING;
            loadFn.call( cc.loader,  route , ( err , tex )=>{
                if(err){
                    console.log(err);
                    console.log(route);
                    this.__state = STATE.FAIL;
                }else{
                    this._res = tex ; // new cc.AudioClip(tex);
                    this.__state = STATE.SUCCESS;
                }

                this._doHandlers();
            } );
        }else if(  _d['type'] == "PreFab" ){
            this.__state = STATE.LOADING;
            loadFn.call( cc.loader,  route , cc.Prefab , ( err , fab )=>{
                if(err){
                    console.log(err);
                    console.log(route);
                    this.__state = STATE.FAIL;
                }else{
                    this._res = fab;
                    this.__state = STATE.SUCCESS;
                }

                this._doHandlers();
            });
        }
    }
    //执行回调
    _doHandlers(){
        this._loadedHandlers = this._loadedHandlers || [];
        for(let i in this._loadedHandlers){
            this._loadedHandlers[i] && this._loadedHandlers[i]( this._res )
        }
        this._loadedHandlers = [];
    }
    //加载完成后等待执行的回调函数
    _addHandler( fn ){
        this._loadedHandlers = this._loadedHandlers || [];
        this._loadedHandlers.push(fn);
    }
}

module.exports = Res;  